package main

import (
	"fmt"
	"time"
)

func main() {

	t := time.Now() //2019-07-31 13:55:21.3410012 +0800 CST m=+0.006015601
	fmt.Println(t.Format("20060102150405"))

	//当前时间戳
	t1 := time.Now().Unix() //1564552562
	fmt.Println(t1)
	//时间戳转化为具体时间
	fmt.Println(time.Unix(t1, 0).String())

	//基本格式化的时间表示
	fmt.Println(time.Now().String()) //2019-07-31 13:56:35.7766729 +0800 CST m=+0.005042501

	fmt.Println("获取当天的时间：", time.Now().Format("2006-01-02"))           //2019-07-31
	fmt.Println("当前时间的时间为：", time.Now().Format("2006-01-02 15:04:05")) //2019-07-31 13:57:52
	fmt.Println("当前的时间戳为：", time.Now().Unix())
	fmt.Println(time.Unix(1596605313, 0).Format("2006-01-02 15:04:05")) //时间戳转化成时间
	fmt.Println("字符串转化成时间:", TimeStrToTimeStamp())
	//获取第几周
	year, week := time.Now().ISOWeek()
	fmt.Println(year, week)
	//获取年、月、日
	year, month, day := DateYmdInts()
	fmt.Println(year, month, day)

	data1, data2, err := GetStartTimeAndEndTime("07:44:57", "11:44:57")
	fmt.Println("data:", data1/3600, data2/3600, err)
	fmt.Println(time.Now().Format("2006-01-02"))
	GetTodayBeginTime()
}

// 获取距离当前的开始时间和结束时间
func GetStartTimeAndEndTime(startTime, endTime string) (int64, int64, error) {
	startTime = GetTodayDate() + " " + startTime
	endTime = GetTodayDate() + " " + endTime
	startTimeStamp, err := GetTimeStampByTimeStr(startTime)
	if err != nil {
		return 0, 0, err
	}
	endTimeStamp, err := GetTimeStampByTimeStr(endTime)
	if err != nil {
		return 0, 0, err
	}
	startTimes := startTimeStamp - GetNowTimeStamp()
	endTimes := endTimeStamp - GetNowTimeStamp()
	fmt.Println("data1:", startTimes, endTimes)
	if startTimes < 0 {
		startTimes = startTimes + 86400
	}
	if endTimes < 0 {
		endTimes = endTimes + 86400
	}
	return startTimes, endTimes, nil
}

func GetTodayDate() string {
	return time.Now().Format("2006-01-02")
}

func GetNowTimeStamp() int64 {
	return time.Now().Unix()
}

func GetTimeStampByTimeStr(toBeCharge string) (int64, error) {
	timeLayout := "2006-01-02 15:04:05"
	loc, _ := time.LoadLocation("Local")
	theTime, err := time.ParseInLocation(timeLayout, toBeCharge, loc)
	if err != nil {
		return 0, err
	}
	sr := theTime.Unix()
	return sr, nil
}

func TimeStrToTimeStamp() int64 {
	//获取本地location
	toBeCharge := "2020-08-05 09:44:57"                             //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
	timeLayout := "2006-01-02 15:04:05"                             //转化所需模板
	loc, _ := time.LoadLocation("Local")                            //重要：获取时区
	theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
	sr := theTime.Unix()                                            //转化为时间戳 类型是int64
	fmt.Println("aa:", sr)
	return sr
}

func GetTodayBeginTime() {
	now := time.Now().Unix()
	todayBeginTime := (now/86400)*86400 - 28800
	fmt.Println(todayBeginTime)

}

// 时间戳转年月日 时分秒
func DateFormat(timestamp int) string {
	tm := time.Unix(int64(timestamp), 0)
	return tm.Format("2006-01-02 15:04")
}

//时间戳转年月日
func DateFormatYmd(timestamp int) string {
	tm := time.Unix(int64(timestamp), 0)
	return tm.Format("2006-01-02")
}

//获取当前年月
func DateYmFormat() string {
	tm := time.Now()
	return tm.Format("2006-01")
}

//获取年月日时分秒（字符串类型）
func DateNowFormatStr() string {
	tm := time.Now()
	return tm.Format("2006-01-02 15:04:05")
}

//时间戳
func DateUnix() int {
	t := time.Now().Local().Unix()
	return int(t)
}

//获取年月日时分秒(time类型)
func DateNowFormat() time.Time {
	tm := time.Now()
	return tm
}

//获取第几周
func DateWeek() int {
	_, week := time.Now().ISOWeek()
	return week
}

//获取年、月、日
func DateYMD() (int, int, int) {
	year, month, day := DateYmdInts()
	return year, month, day
}

// 获取年月日
func DateYmdFormat() string {
	tm := time.Now()
	return tm.Format("2006-01-02")
}

// 获取日期的年月日
func DateYmdInts() (int, int, int) {
	timeNow := time.Now()
	year, month, day := timeNow.Date()
	return year, int(month), day
}

// 获取当前剩余的时间
func GetTimes() {
	todayLast := time.Now().Format("2006-01-02") + " 23:59:59"
	todayLastTime, _ := time.ParseInLocation("2006-01-02 15:04:05", todayLast, time.Local)
	remainSecond := time.Duration(todayLastTime.Unix()-time.Now().Local().Unix()) * time.Second
	fmt.Println(remainSecond)
}
